package com.fyzk.activity.lottery.jc.football.newJz.data;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tyjc.lottery.R;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by fanxs11 on 16/4/26.
 */
public class AnalysisAdapter extends BaseExpandableListAdapter implements QQListView.QQHeaderAdapter{

    private ArrayList<AnalysisViewInfo> infos = new ArrayList<>();
    private Context context;
    private QQListView listView;

    public AnalysisAdapter(Context context, ArrayList<AnalysisViewInfo> infos, QQListView listView){
        this.context = context;
        this.infos = infos;
        this.listView = listView;
    }

    @Override
    public int getGroupCount() {
        return infos.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return infos.get(groupPosition);
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder groupViewHolder;
        if(convertView==null){
            groupViewHolder = new GroupViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.qqlist_group, null);
            groupViewHolder.groupName_tv = (TextView) convertView.findViewById(R.id.groupto);
            groupViewHolder.icon_img = (ImageView) convertView.findViewById(R.id.group_icon);
            groupViewHolder.jiantou_img = (ImageView) convertView.findViewById(R.id.group_jiantou);

            convertView.setTag(groupViewHolder);
        } else {
            groupViewHolder = (GroupViewHolder) convertView.getTag();
        }

        groupViewHolder.groupName_tv.setText(""+infos.get(groupPosition).group);
        groupViewHolder.icon_img.setImageResource(infos.get(groupPosition).icon);
        if (isExpanded) {
            groupViewHolder.jiantou_img.setImageResource(R.drawable.jc_jiantou_down);
        }
        else{
            groupViewHolder.jiantou_img.setImageResource(R.drawable.jc_jiantou_up);
        }
        return convertView;
    }

    private class GroupViewHolder {
        TextView groupName_tv;
        ImageView icon_img;
        ImageView jiantou_img;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
//        return infos.get(groupPosition).childInfos.size();
        return 1;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return infos.get(groupPosition).childInfos.get(childPosition);
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {

//        ChildViewHolder childViewHolder;
//        AnalysisViewInfo info = infos.get(groupPosition);
//
//        if (convertView == null) {
//            childViewHolder = new ChildViewHolder();
//            convertView = LayoutInflater.from(context).inflate(R.layout.jc_analysis_child, null);
//            childViewHolder.head_rl = (RelativeLayout) convertView.findViewById(R.id.jc_analysis_child_rl);
//            childViewHolder.content_ll = (LinearLayout) convertView.findViewById(R.id.jc_analysis_child_ll);
//
//            convertView.setTag(childViewHolder);
//        } else {
//            childViewHolder = (ChildViewHolder) convertView.getTag();
//        }
//        if (info.type == 0) {   //历史交锋
//
//            childViewHolder.head_rl.setVisibility(View.GONE);
//
//        } else if (info.type == 1) {    // 近期战绩
//            if (childPosition == 0 || childPosition == 10) {
//                childViewHolder.content_ll.setVisibility(View.GONE);
//            } else {
//                childViewHolder.head_rl.setVisibility(View.GONE);
//            }
//
//        } else if (info.type == 2) {    // 未来比赛
//            if (childPosition == 0 || childPosition == 5) {
//                childViewHolder.content_ll.setVisibility(View.GONE);
//            } else {
//                childViewHolder.head_rl.setVisibility(View.GONE);
//            }
//        }



        return infos.get(groupPosition).view;
    }

    private class ChildViewHolder {
        RelativeLayout head_rl;
        LinearLayout content_ll;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }


    // ===========
    @Override
    public int getQQHeaderState(int groupPosition, int childPosition) {
        final int childCount = getChildrenCount(groupPosition);
        if(childPosition == childCount - 1){
            return PINNED_HEADER_PUSHED_UP;
        }
        else if(childPosition == -1 && !listView.isGroupExpanded(groupPosition)){
            return PINNED_HEADER_GONE;
        }
        else{
            return PINNED_HEADER_VISIBLE;
        }
    }

    @Override
    public void configureQQHeader(View header, int groupPosition, int childPosition, int alpha) {
        TextView textView = (TextView) header.findViewById(R.id.groupto);
        ImageView icon_img = (ImageView) header.findViewById(R.id.group_icon);
        textView.setText(""+infos.get(groupPosition).group);
        icon_img.setImageResource(infos.get(groupPosition).icon);
    }

    private HashMap<Integer,Integer> groupStatusMap = new HashMap<Integer, Integer>();

    @Override
    public void setGroupClickStatus(int groupPosition, int status) {
        groupStatusMap.put(groupPosition, status);
    }

    @Override
    public int getGroupClickStatus(int groupPosition) {
        if(groupStatusMap.containsKey(groupPosition)){
            return groupStatusMap.get(groupPosition);
        }else{
            return 0;
        }
    }
    //==========
}
